{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2a7feaf4",
   "metadata": {},
   "source": [
    "---------\n",
    "\n",
    "* _记录人：于铠骏_\n",
    "* _时间：2021-11-18_\n",
    "\n",
    "---------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "603f1290",
   "metadata": {},
   "source": [
    "## 本周内容\n",
    "### 1. 字典的实践（一）\n",
    "\n",
    "\n",
    "\n",
    "<big>\n",
    "</big>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b43c807d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:hello world\n",
      "{'a': 0, 'e': 1, 'i': 0, 'o': 2, 'u': 0}\n"
     ]
    }
   ],
   "source": [
    "# 在列表基础上（列表可以统计出现的内容有哪些\n",
    "# 字典可统计出现的内容 + 出现的次数（词频）\n",
    "\n",
    "vowels = [\"a\", \"e\", \"i\", \"o\", \"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "\n",
    "# 利用字典创建的方式dict_name[key]=value\n",
    "\n",
    "# 当然用循环的方法更快：\n",
    "# for i in vowels:\n",
    "#     found[i] = 0\n",
    "\n",
    "# 还有更高级的方法：\n",
    "found = found.fromkeys(vowels,0)\n",
    "    \n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        found[i] +=1\n",
    "print(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8fe08a52",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 1, 'i': 0, 'o': 2, 'u': 0}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b99bef0",
   "metadata": {},
   "source": [
    "#### 1. 字典的高级方法：fromkeys()\n",
    "<br>\n",
    "<big>\n",
    "    fromkeys() 大概就是把一个序列作为这个字典的keys，<br>\n",
    "    然后初始化这个字典的<b>所有值</b>value（默认为none），也可以指定一个值，如fromkeys(list_name,1)，👈将值指定为1\n",
    "</big>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "240dc7ee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function fromkeys:\n",
      "\n",
      "fromkeys(iterable, value=None, /) method of builtins.type instance\n",
      "    Create a new dictionary with keys from iterable and values set to value.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(found.fromkeys)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "39ebb05b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Alex': 0, 'Marry': 0, 'Jack': 0, 'Jan': 0}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names_list = ['Alex', 'Marry', 'Jack', 'Jan']\n",
    "names_age_dict = {}\n",
    "names_age_dict.fromkeys(names_list,0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a7025c0",
   "metadata": {},
   "source": [
    "### 2. 字典的实践（二）\n",
    "\n",
    "* 课本\n",
    "\n",
    "#### 2. 字典的高级方法：setdefault()\n",
    "<br>\n",
    "<big>\n",
    "    <b>分为两种情况</b>\n",
    "    <br>\n",
    "    1. 如果字典中还没有该内容（item），则在字典中以这个item新增一个key，值默认为None，当然也可以指定一个值\n",
    "    <br>\n",
    "    2. 如果字典中有这个内容则不操作，返回这个key所对应的值\n",
    "</big>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d6d11206",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function setdefault:\n",
      "\n",
      "setdefault(key, default=None, /) method of builtins.dict instance\n",
      "    Insert key with a value of default if key is not in the dictionary.\n",
      "    \n",
      "    Return the value for key if key is in the dictionary, else default.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names_age_dict.setdefault)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f2824ca2",
   "metadata": {},
   "outputs": [],
   "source": [
    "people = {}\n",
    "\n",
    "people['Ford'] = {\"Name\":'Ford Prefect',\n",
    "                  \"Gender\":'Male',\n",
    "                  \"Occupation\":'Researcher',\n",
    "                  \"Home Planet\":'Betelgeuse Seven'}\n",
    "people['Arthur'] = {\"Name\":'Arthur Dent',\n",
    "                  \"Gender\":'Male',\n",
    "                  \"Occupation\":'Sandwich-Maker',\n",
    "                  \"Home Planet\":'Earth'}\n",
    "people['Tricia'] = {\"Name\":'Tricia McMillan',\n",
    "                    \"Gender\":'Female',\n",
    "                    \"Occupation\":'Mathematician',\n",
    "                    \"Home Planet\":'Earth'}\n",
    "people['Marvin'] = {\"Name\":'Marvin',\n",
    "                    \"Gender\":'Unknown',\n",
    "                    \"Occupation\":'Paranoid Android',\n",
    "                    \"Home Planet\":'Unknown'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2ae5889a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Ford': {'Name': 'Ford Prefect',\n",
       "  'Gender': 'Male',\n",
       "  'Occupation': 'Researcher',\n",
       "  'Home Planet': 'Betelgeuse Seven'},\n",
       " 'Arthur': {'Name': 'Arthur Dent',\n",
       "  'Gender': 'Male',\n",
       "  'Occupation': 'Sandwich-Maker',\n",
       "  'Home Planet': 'Earth'},\n",
       " 'Tricia': {'Name': 'Tricia McMillan',\n",
       "  'Gender': 'Female',\n",
       "  'Occupation': 'Mathematician',\n",
       "  'Home Planet': 'Earth'},\n",
       " 'Marvin': {'Name': 'Marvin',\n",
       "  'Gender': 'Unknown',\n",
       "  'Occupation': 'Paranoid Android',\n",
       "  'Home Planet': 'Unknown'}}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "people"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "8b4bedf4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Tricia McMillan'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "people['Tricia']['Name']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "627e1da9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Unknown'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "people['Marvin']['Home Planet']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "a89b48c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['Name', 'Gender', 'Occupation', 'Home Planet'])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询字典中的keys\n",
    "\n",
    "people['Marvin'].keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a53fcd9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
